package com.uwlaser.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uwlaser.annotation.Log;
import com.uwlaser.domain.CraftPathDetail;
import com.uwlaser.service.CraftPathDetailService;
import com.uwlaser.service.dto.CraftPathDetailDto;
import com.uwlaser.service.dto.CraftPathDetailQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.util.List;
import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "工艺路线详情管理")
@RequestMapping("/api/craftPathDetail")
public class CraftPathDetailController {

    private final CraftPathDetailService craftPathDetailService;

    @GetMapping
    @Log("查询工艺路线详情-分页")
    @ApiOperation("查询工艺路线详情-分页")
    @PreAuthorize("@el.check('craftPathDetail:list')")
    public ResponseEntity query(CraftPathDetailQueryParam query, Pageable pageable){
        return new ResponseEntity<>(craftPathDetailService.queryAll(query,pageable),HttpStatus.OK);
    }

    @GetMapping("/list")
    @Log("查询工艺路线详情-列表")
    @ApiOperation("查询工艺路线详情-列表")
    @PreAuthorize("@el.check('craftPathDetail:list')")
    public ResponseEntity list(CraftPathDetailQueryParam query){
        return new ResponseEntity<>(craftPathDetailService.queryAll(query),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增工艺路线详情")
    @ApiOperation("新增工艺路线详情")
    @PreAuthorize("@el.check('craftPathDetail:add')")
    public ResponseEntity create(@Validated @RequestBody CraftPathDetailDto resources){
        return new ResponseEntity<>(craftPathDetailService.insert(resources),HttpStatus.CREATED);
    }

    @PostMapping("/addBatch")
    @Log("批量新增工艺路线详情")
    @ApiOperation("批量新增工艺路线详情")
    @PreAuthorize("@el.check('craftPathDetail:add')")
    public ResponseEntity addBatch(@Validated @RequestBody List<CraftPathDetail> resources){
        //删除后添加
        CraftPathDetail craftPathDetail = new CraftPathDetail();
        craftPathDetail.setPathId(resources.get(0).getPathId());
        craftPathDetailService.remove(new QueryWrapper<>(craftPathDetail));
        return new ResponseEntity<>(craftPathDetailService.saveBatch(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改工艺路线详情")
    @ApiOperation("修改工艺路线详情")
    @PreAuthorize("@el.check('craftPathDetail:edit')")
    public ResponseEntity update(@Validated @RequestBody CraftPathDetailDto resources){
        craftPathDetailService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除工艺路线详情")
    @ApiOperation("删除工艺路线详情")
    @PreAuthorize("@el.check('craftPathDetail:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        craftPathDetailService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出工艺路线详情")
    @ApiOperation("导出工艺路线详情")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('craftPathDetail:list')")
    public void download(HttpServletResponse response, CraftPathDetailQueryParam query) throws IOException {
        craftPathDetailService.download(craftPathDetailService.queryAll(query), response);
    }*/

}
